package br.elsa.survey.domain.questions;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * JPA {@link Entity} that abstracts a survey question.
 * 
 * @author diego
 * @since 2010-09-27
 */
@Entity
@Table(name = "questions", schema = "survey")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class Question implements Serializable {

	private static final long serialVersionUID = 124517627040626962L;

	/**
	 * Question unique identifier
	 * <p>
	 * It will resolve to a primary key in a OR mapping.
	 * </p>
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private Integer id;

	/**
	 * Question text
	 */
	@NotEmpty(message = "The question text can not be empty")
	private String text;

	/**
	 * Date that the question was created
	 */
	@Column(name = "creation_date")
	@Temporal(TemporalType.TIMESTAMP)
	private Date creationDate;

	@Enumerated(EnumType.STRING)
	private Level level;

	public Question() {
		super();
	}

	@PrePersist
	public void prePersist() {
		this.creationDate = new Date(System.currentTimeMillis());
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getText() {
		return this.text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public void setLevel(Level level) {
		this.level = level;
	}

	public Level getLevel() {
		return level;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Question)) {
			return false;
		}

		if (this == obj) {
			return true;
		}

		Question question = (Question) obj;
		if ((this.getId() == null && question.getId() == null) || this.getId().equals(question.getId())) {

			return true;
		}

		return false;
	}
}
